﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrionApplication.OrionLib.Dao;
using OrionApplication.OrionLib.Orion;
using OrionApplication.OrionLib;

namespace OrionApplication.OrionForm
{
    public partial class MouvementTeteForm : OrionBaseDoubleForm
    {
        public Int32 ClientId = 0;

        public MouvementTeteForm()
            : base()
        {
            dao = new DaoMouvementTete();
            InitializeComponent();
            this.kCbMouvement.Cb.SelectedIndexChanged += new System.EventHandler(this.Cb_SelectedIndexChanged);
        }

        public MouvementTeteForm(string titre)
            : base(titre, "Etat")
        {
            dao = new DaoMouvementTete();
            InitializeComponent();
            this.kCbMouvement.Cb.SelectedIndexChanged += new System.EventHandler(this.Cb_SelectedIndexChanged);
        }

        private void DupliqueEnregistrement()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                if (OrionOutils.ChoixOuiNon(OrionConfBase.ConfirmDuplique) == DialogResult.Yes)
                {
                    MouvementTete m = ((DaoMouvementTete)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                    m = ((DaoMouvementTete)dao).Insert(m);
                    this.RempliKDgv();
                    this.FocusSur(m.OrionId);
                }
            }
            else
                OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
        }

        private void RempliForm(MouvementTete m)
        {
            kCbMouvement.Cb.SelectedValue = m.OrionMouvementId;
            if (m.Date != null)
            {
                kDtDate.Value = m.Date.Value;
            }
            kCbClient.SelectedValue = m.OrionClientId;
            kTbCommentaire.Text = m.Commentaire;
        }

        private void SauvegardeForm()
        {
            MouvementTete m = this.RempliObject();
            if (kLblMode.Text == OrionConfBase.OrionModeInsert)
                m = ((DaoMouvementTete)dao).Insert(m);
            else
            {
                if (kDgv.SelectedRows.Count > 0)
                {
                    m.OrionId = Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value);
                    m = ((DaoMouvementTete)dao).Update(m);

                }
                else
                    OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
            }
            this.RempliKDgv();
            this.FocusSur(m.OrionId);
        }

        private MouvementTete RempliObject()
        {
            MouvementTete m = new MouvementTete();
            m.OrionMouvementId = Convert.ToInt32(kCbMouvement.Cb.SelectedValue);
            m.Date = kDtDate.Value;
            if(kCbClient.Enabled)
                m.OrionClientId = Convert.ToInt32(kCbClient.SelectedValue);
            m.Commentaire = kTbCommentaire.Text;
            return m;
        }

        public void RempliClient()
        {
            DaoClient daoC = new DaoClient();
            kCbClient.DataSource = daoC.SelectAll();
        }

        public void RowEnter()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                MouvementTete m = ((DaoMouvementTete)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                this.RempliForm(m);
            }
        }

        public void OuvreDetailContrat()
        {
            if (this.kLblMode.Text == "MAJ" && this.NbLigneDgSelected == 1)
            {
                MouvementLigneForm af = new MouvementLigneForm("Détail ");
                af.OrionTeteId = this.OrionIdGdvSelected;
                af.MdiParent = this.MdiParent;
                af.NameFormA = "MouvementLigneForm";
                af.Show();
            }
            else
            {
                OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
            }
        }

        /// <summary>
        /// Permet de remplir le grid avec le DAO en cours
        /// </summary>
        public override void RempliKDgv()
        {
            if (this.ClientId == 0)
            {
                DataSet ds = ((DaoMouvementTete)dao).SelectAllDSView();
                if (ds.Tables.Count > 0)
                {
                    kDgv.DataSource = ds.Tables[0];
                    kDgv.Columns["OrionId"].Visible = false;
                }
            }
            else
            {
                DataSet ds = ((DaoMouvementTete)dao).SelectAllClientDSView(this.ClientId);
                if (ds.Tables.Count > 0)
                {
                    kDgv.DataSource = ds.Tables[0];
                    kDgv.Columns["OrionId"].Visible = false;
                }
            }
        }

        private void Cb_SelectedIndexChanged(object sender, EventArgs e)
        {
            OrionTable ot = new DaoOrionTable().SelectById(Convert.ToInt32(this.kCbMouvement.Cb.SelectedValue));
            if (ot.Text2 == "FOURNISSEUR")
                kCbClient.Enabled = false;
            else
                kCbClient.Enabled = true;
        }

        private void btnSpecGoToClient_Click(object sender, EventArgs e)
        {
            ClientForm cf = new ClientForm("Gestion des clients");
            cf.MdiParent = this.MdiParent;
            cf.NameFormA = "ClientForm";
            cf.Show();
            cf.FocusSur(Convert.ToInt32(this.kCbClient.SelectedValue));
        }
    }
}
